/**
 * @description promise.race 实现
 * @param   {Array}  promises Promise数组
 * @returns {object} Promise对象
 */
const promiseRace = (promises) => {
  return new Promise((resolve, reject) => {
    const length = promises.length;
    for (let i = 0; i < length; i++) {
      const promise = promises[i];
      if (promise.then && typeof promise.then === "function") {
        promise.then((res) => {
          resolve(res);
        });
      }
    }
  });
};

// 测试
const sleep = (seconds) =>
  new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve(seconds);
    }, seconds);
  });

promiseRace([sleep(5000), sleep(3000), sleep(1000)]).then((res) => {
  console.log("res :>> ", res);
});
